package com.example.springstudyvue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springstudyvue.entity.User;
import com.example.springstudyvue.mapper.UserMapper;
import com.example.springstudyvue.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author linKe
 * @since 2021-11-26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public Boolean addUser(User user) {
        if (user.getPassword() == null) {
            user.setPassword("123456");
        }
        return userMapper.insert(user) > 0;
    }

    @Override
    public Boolean updateUser(User user) {
        return userMapper.updateById(user) > 0;
    }

    @Override
    public boolean deleteUser(Long userId) {
        return userMapper.deleteById(userId) > 0;
    }

    @Override
    public User login(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername())
                .eq("password", user.getPassword())
                .eq("role", user.getRole());
        User userA = userMapper.selectOne(queryWrapper);
        if (userA == null) {
            return null;
        }else {
            return  userMapper.selectOne(queryWrapper);

        }

//        生成token
//        String token = TokenUtils.genToken(res);
//        res.setToken(token);


    }

    @Override
    public boolean register(User user) {
        User res = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getAccount, user.getAccount()));
        return res == null && userMapper.insert(user) > 0;
    }

    @Override
    public User findUser(Long userId) {
        return userMapper.selectById(userId);
    }


    @Override
    public Page<User> findPage(Integer pageNum,
                               Integer pageSize,
                               String search) {

        return userMapper.findPage(new Page<>(pageNum, pageSize), search);
    }
}
